var urltest = sessionStorage.getItem("urltest");
var userId = sessionStorage.getItem("userId");
var fullname = sessionStorage.getItem("fullname");

$(function() {
	if(userId == "" || userId == undefined || userId == null) {
		top.location.href = "../login.html";
		
	}
});
//加载数据
function ajax_data(data) {
	$.ajax({
		contentType: "application/json; charset=utf-8",
		type: "POST",
		url: urltest + 'verify/financial/selectSettleOrderList',
		data: data,
		async: false,
		dataType: "json",
		success: function(msg) {
			console.log(msg);
			if(msg.responseCode == 200) {
				$("#foot_sure").show();
				$(".foot_zong").show();
				var bb = JSON.parse(msg.returnString);
				$(".wx_count").html(bb.total.weixincount);
				$(".wx_rate").html(bb.total.weixin);
				$(".zfb_count").html(bb.total.alicount);
				$(".zfb_rate").html(bb.total.ali);
				$(".zong_count").html(bb.total.count);
				$(".zong_rate").html(bb.total.paymentamt);
				var table_msg = msg.returnString;
				createShowingTable(table_msg);
			} else {
				alert(msg.resoponseDisp)
			}
		},
		error: function() {
			alert("获取信息失败");
		}
	});
}

function createShowingTable(bb) {
	//获取后台传过来的jsonData,并进行解析  
	//此处需要让其动态的生成一个table并填充数据 
	if(bb.total == 0) {
		$("#tbbb").html("");
	} else {

		var bb = JSON.parse(bb);
		console.log(bb);
		var tableStr = "";
		var len = bb.list.length;

		if(len == 0) {
			var wuxiaoxi = '<p>' + "暂无内容" + '</p>'
			$("#tbbb").html(wuxiaoxi);
		} else {
			var aa = 0;
			for(var i = 0; i < len; i++) {

				//				debugger
				var num = parseInt(i) + parseInt(1);
				tableStr = tableStr + '<tr>' +
					'<th>' + num + '</th>' +
					'<th>' + bb.list[i].merchantname + '</th>' +
					'<th>' + bb.list[i].areaname + '</th>' +
					'<th>' + bb.list[i].weixincount + '</th>' +
					'<th>' + bb.list[i].weixin + '</th>' +
					'<th>' + bb.list[i].alicount + '</th>' +
					'<th>' + bb.list[i].ali + '</th>' +
					'<th>' + bb.list[i].count + '</th>' +
					'<th>' + bb.list[i].paymentamt + '</th>' +
					'</tr>';

			}

			//将动态生成的table添加的事先隐藏的div中.  
			$("#tbbb").html(tableStr);

		}

	}
}

//模糊查询
$('#search_chaxun').click(function() {
	var star_time = $('.input_1').val();
	var reque = function() {
		return {
			endday: star_time,
		}
	}
	rq1 = reque();
	var req = function() {
		return {
			requestString: "",
			userId: userId,
			fullname: fullname
		}
	}
	requestObj1 = req();
	requestObj1.requestString = JSON.stringify(rq1);
	var req3 = JSON.stringify(requestObj1);
	ajax_data(req3)

});
//点击确定生成
$("#sure_export").click(function() {
	$('#myModal').modal('show')
})
$(".sure_sub").click(function() {
	var billcode = $("#djh_wy").val();
	var billdate = $("#data_djrq").val();
	var payenddate = $('.input_1').val();

	if(billcode == "" || billdate == "") {
		alert("请确保单据号和单据日期都已填写")
	} else {
		var requestString = JSON.stringify({
			billcode: billcode,
			billdate: billdate,
			payenddate: payenddate
		})
		var req3 = JSON.stringify({
			requestString: requestString,
			userId: userId,
			fullname: fullname
		})
		console.log(req3)
		$.ajax({
			contentType: "application/json; charset=utf-8",
			type: "POST",
			url: urltest + 'verify/financial/addOrgainzeSettle',
			data: req3,
			async: false,
			dataType: "json",
			success: function(msg) {
				console.log(msg);
				if(msg.responseCode == 200) {
					alert("生成结算单成功");
					$('#myModal').modal('hide')
				} else {
					alert(msg.resoponseDisp)
				}
			},
			error: function() {
				alert("获取信息失败");
			}
		});
	}

})